﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AviarySDK;
using Microsoft.Phone.Tasks;
using TM.QWeibo.SDK.Objects;

namespace TM.QWeibo.Client.Tweet
{
    public partial class Picture : PhoneApplicationPage
    {
        public Picture()
        {
            InitializeComponent();
        }

        Helper.DraftHelper draftHelper= new Helper.DraftHelper();

        private bool isCompleted = true;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (draftHelper.TweetImage != null && isCompleted)
            {
                isCompleted = false;
                var aviaryTask = new AviaryTask(draftHelper.TweetImage.FileStream, themeColor: "52A6EF");
                aviaryTask.Completed += aviaryTask_Completed;
                aviaryTask.Show();
            }
        }
        

        void aviaryTask_Completed(object sender, AviaryTaskResultArgs e)
        {
            if (e.AviaryResult == AviaryResult.OK)
            {
                var stream = new MemoryStream();
                e.PhotoResult.SaveJpeg(stream, e.PhotoResult.PixelWidth, e.PhotoResult.PixelHeight, 0, 100);
                stream.Seek(0, SeekOrigin.Begin);
                draftHelper.TweetImage=new UploadFile(draftHelper.TweetImage.FileName,stream);
            }
        }
    }
}